package com.justcan.demo.cartoon.utils;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.Transformation;

import com.justcan.demo.cartoon.R;
import com.justcan.demo.cartoon.utils.anim.AnimObject;

/**
 * 小雾霾
 * @author zhanghuijun
 *
 */
public class SmallFogObject extends AnimObject {

	/**
	 * 宽高
	 */
	private float mWidth = 0;
	private float mHeight = 0;
	/**
	 * 坐标
	 */
	private int mX = 0;
	private int mY = 0;
	/**
	 * 是否已经初始化
	 */
	private boolean mHasInit = false;
	/**
	 * 图
	 */
	private Bitmap mBitmap = null;
	/**
	 * 绘制Rect
	 */
	private Rect mSrcRect = null;
	private Rect mDstRect = null;
	/**
	 * Paint
	 */
	private Paint mPaint = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.FILTER_BITMAP_FLAG);
	/**
	 * 上升动画
	 */
	private SmallFogTracAnimation mUpAnimation = null;
	/**
	 * FogObject
	 */
	private FogObject mFogObject = null;

	public SmallFogObject(View mRootAnimView, Context mContext) {
		super(mRootAnimView, mContext);
		if (getAnimSceneWidth() != 0 && getAnimSceneHeight() != 0) {
			init();
		}
	}

	@Override
	public void onSizeChange(int w, int h) {
		super.onSizeChange(w, h);
		init();
	}

	/**
	 * 初始化
	 */
	private void init() {
		if (!mHasInit) {
			mBitmap = BitmapFactory.decodeResource(getContext().getResources(), R.drawable.rocket_fog);
			int mSceneWidth = getAnimSceneWidth();
			int mSceneHeight = getAnimSceneHeight();
			mWidth = mSceneWidth;	// 宽度
			mHeight = mWidth * 0.404f;		// 根据图片计算合理的高度
			mX = 0;			// 定义初始位置
			mY = (int) (mSceneHeight - mHeight);
			mSrcRect = new Rect(0, 0, mBitmap.getWidth(), mBitmap.getHeight());
			mDstRect = new Rect(mX, mY, (int) (mWidth + mX), (int) (mHeight + mY));
			mHasInit = true;
		}
	}

	@Override
	public void logic(long animTime, long deltaTime) {
		if (mFogObject != null) {
			int fogHeight = mFogObject.getFogRect().height();
			int fogTop = mFogObject.getFogRect().top;
			if (mUpAnimation == null) {
				mUpAnimation = new SmallFogTracAnimation();
				mUpAnimation.mStartX = mX;
				mUpAnimation.mStartY = mY;
				mUpAnimation.mEndX = mX;
				mUpAnimation.mEndY = (int) (getAnimSceneHeight() - mHeight - fogHeight);
				mUpAnimation.setDuration(500);
			}
			mUpAnimation.getTransformation(animTime, null);
			mDstRect.set(mX, mY, (int) (mWidth + mX), fogTop);
		}
	}

	@Override
	public void draw(Canvas canvas, int sceneWidth, int sceneHeight) {
		if (mHasInit) {
			canvas.drawBitmap(mBitmap, mSrcRect, mDstRect, mPaint);
		}
	}

	public void setFogObject(FogObject object) {
		mFogObject = object;
	}

	/**
	 * 拉伸动画
	 */
	class SmallFogTracAnimation extends Animation {

		/**
		 * 起始拉伸位置
		 */
		public int mStartX = 0;
		public int mStartY = 0;
		/**
		 * 预期拉伸位置
		 */
		public int mEndX = 0;
		public int mEndY = 0;

		@Override
		protected void applyTransformation(float interpolatedTime,
										   Transformation t) {
			// 改变真实的位置坐标
			mX = (int) (mStartX + (mEndX - mStartX) * interpolatedTime);
			mY = (int) (mStartY + (mEndY - mStartY) * interpolatedTime);
		}
	}
}
